/**
 * 销售数据
 * Created by douxc on 2015/4/22.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('SaleDataCtrl', ['$scope', '$filter', 'MHService', '$location', '$routeParams',
      function ($scope, $filter, MHService, $location, $routeParams) {
        $scope.building = {
          estateName: $location.search().name
        };
        /**
         * 新建销售数据
         */
        $scope.saleAdd = function () {
          $location.path('/sale/add');
        };
        MHService.getReq({
          url: MHService.URL.sales_query + $routeParams['Id']
        }, function (data) {
          if (data['obj']) {
            var _data = data['obj'];
            var tdata = angular.copy(_data);
            console.log(tdata);
            tdata.finishTime = new Date(_data.finishTime.time);
            //如果返回的数据中没有名称，则使用默认名称
            if (!tdata.estateName) {
              tdata.estateName = $location.search().name;
            }
            $scope.building = tdata;
          }
        });
        /**
         * 提交销售数据
         * @param data
         */
        $scope.salesAdd = function (_data) {
          var data = angular.copy(_data);
          data.estateId = $routeParams['Id'];//楼盘Id
          data.finishTime = $filter('date')(_data.finishTime, 'yyyy-MM-dd');
          MHService.postReq({url: MHService.URL.sales_add, _req: data}, function (result) {
            if (result['success']) {
              MHService.Toast('提交成功');
            } else {
              MHService.Toast('操作失败，请稍后重试');
            }
          });
        }
      }]);
  };
});
